//
//  TbInfoNewView.swift
//  SlaughterClient
//
//  Created by 邱仙凯 on 2024/8/5.
//  Copyright © 2024 zwb. All rights reserved.
//

import UIKit
class TbInfoNewView: UIView {
    var topLine:UIView = {
        var v = UIView()
        v.backgroundColor = UIColor(kshex: "f6f6f6")
        return v
    }()
    var bottomLine:UIView = {
        var v = UIView()
        v.backgroundColor = UIColor(kshex: "f6f6f6")
        return v
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .white
        self.addSubview(topLine)
        self.addSubview(bottomLine)
        self.addSubview(content1)
        self.addSubview(contLinkLabel)
        self.addSubview(contContentLabel)
        contContentLabel.textColor = UIColor(kshex: tenderBlue)
         
        content1.snp.makeConstraints { make in
            make.edges.equalTo(UIEdgeInsets(top: 20, left: 10, bottom: 5, right: 10))
        }
        topLine.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(10)
        }
        bottomLine.snp.makeConstraints { make in
            make.bottom.left.right.equalToSuperview()
            make.height.equalTo(10)
        }
        content1.enableCopy = true
        content1.font = UIFont.systemFont(ofSize: fontx)
        content1.numberOfLines = 0
        content1.textAlignment = .left
        content1.preferredMaxLayoutWidth = KSGlobal.screenW - 32
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    var fontx:CGFloat = 16
    var content1 = CJLabel(frame: CGRect.zero)
    var contLinkLabel = CJLabel(frame: CGRect.zero)
    var contContentLabel = CJLabel(frame: CGRect.zero)

    var maxKeyLength: CGFloat = 0
    var contentStr:String = "" {
        didSet {
            let paraph = NSMutableParagraphStyle()
            //将行间距设置为28
            paraph.lineSpacing = 6
//            //样式属性集合
//            let attributes = [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 14),
//                              NSAttributedString.Key.paragraphStyle: paraph]
//            self.content1.attributedText = NSAttributedString(string: contentStr, attributes: attributes)
//
          
              
      
        
            // 假设使用相同的字体来计算空格宽度
            let spaceFont = content1.font
            let spaceString = " "
            let spaceAttributes = [NSAttributedString.Key.font: spaceFont]
            let spaceWidth = spaceString.size(withAttributes: spaceAttributes).width
      
            // 分割文本并处理每对键值
            let parts = contentStr.components(separatedBy: "#")
            let attributedString = NSMutableAttributedString()
      
            
            //提前算出最大左边宽度
            for part in parts {
                guard !part.isEmpty else { continue }
                let keyValue = part.components(separatedBy: ":")
                guard keyValue.count >= 2 else { continue }
      
                let key = keyValue[0].trimmingCharacters(in: .whitespacesAndNewlines)
                let keyAttributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: fontx)]
                let keySize = key.size(withAttributes: keyAttributes)
                maxKeyLength = max(maxKeyLength, keySize.width)
            }
            if maxKeyLength < 90 {
                maxKeyLength = 90
            }
      
            for part in parts {
                guard !part.isEmpty else { continue }
                
                let keyOri = part.preChar(char: ":").unwrap
                
                let valueOri = part.replacingOccurrences(of: "\(keyOri):", with: "")
                
              
      
                var key = keyOri.trimmingCharacters(in: .whitespacesAndNewlines)
                let keyAttributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: fontx),NSAttributedString.Key.paragraphStyle: paraph]
                let keySize = key.size(withAttributes: keyAttributes)
              
      
                var value = valueOri.trimmingCharacters(in: .whitespacesAndNewlines)
                if value == "-" {
                    value = ""
                }
                if key == "-" {
                    key = "  "
                }
      
                // 计算空格数
                let spacesCount = Int((maxKeyLength - keySize.width) / spaceWidth)
                let spacesString = String(repeating: " ", count: spacesCount)
      
                let fullLine = NSMutableAttributedString()
                // 把冒号换成空格，有点距离，又符合需求
                fullLine.append(NSAttributedString(string: key + " ", attributes: keyAttributes))
                fullLine.append(NSAttributedString(string: spacesString + value + "\n", attributes: keyAttributes))
      
                attributedString.append(fullLine)
            }
       
      
 
            content1.attributedText = attributedString
            
             
        }
    }
    // 后面复制，与上面同左宽
    var contentLinkStr:String = "" {
        didSet {
            let keyOri = contentLinkStr.preChar(char: ":").unwrap
            let valueOri = contentLinkStr.replacingOccurrences(of: "\(keyOri):", with: "")
            
            contLinkLabel.text = keyOri
           
            
            
            let attributedString = NSMutableAttributedString(string: valueOri)
            attributedString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: NSRange(location: 0, length: attributedString.length))
            attributedString.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 16), range: NSRange(location: 0, length: attributedString.length))
            attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor(kshex: tenderBlue), range: NSRange(location: 0, length: attributedString.length))

            
            contContentLabel.attributedText = attributedString
    
            contLinkLabel.snp.remakeConstraints { make in
                make.left.equalTo(content1)
                make.top.equalToSuperview().offset(20)
                
            }
            contContentLabel.snp.remakeConstraints { make in
                make.left.equalToSuperview().offset(maxKeyLength + 9)
                make.top.equalToSuperview().offset(20)
            }
            
            content1.snp.remakeConstraints { make in
//                make.edges.equalTo(UIEdgeInsets(top: 20, left: 10, bottom: 5, right: 10))
                make.top.equalTo(contLinkLabel.snp.bottom).offset(6)
                make.left.equalToSuperview().offset(10)
                make.right.equalToSuperview().offset(-10)
                make.bottom.equalToSuperview().offset(-5)
            }
        }
    }
    var attributedContentStr:NSAttributedString? {
        didSet {
            if let aContent = attributedContentStr {
                self.content1.attributedText = aContent
            }
           
            
        }
    }
    
  
    
}
 
 
